<?php

declare (strict_types=1);

namespace app\beautiful\controller\cashier;
use app\BaseController;
use app\beautiful\model\LevelModel;
use app\beautiful\model\mall\GoodsCategoryModel;
use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\member\MemberBalanceModel;
use app\beautiful\model\member\MemberCouponModel;
use app\beautiful\model\member\MemberIntegralModel;
use app\beautiful\model\member\MemberModel;
use app\beautiful\model\member\MemberOnceCardItemModel;
use app\beautiful\model\member\MemberOnceCardModel;
use app\beautiful\model\member\MemberSnapUpModel;
use app\common\logic\DataLogic;

class Cash extends BaseController{
    public function getGoodsList()
    {

        $name=$this->request->param('name');
        $category_id=$this->request->param('category_id/d',0);
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
            ['status','=',1],
            ['quota','>',0]
        ];
        if(!empty($name)) $where[]=['name','like','%'.$name.'%'];
        if(!empty($category_id)) $where[]=['category_id','=',$category_id];


        $goods_model= new GoodsModel();
        $goods_model=$goods_model->with(['sku'=>function($query){
            $query->where([
                ['shop_id','=',$this->request->shopId],
                ['is_delete','=',0],
                ['quota','>',0],
                ['is_delete','=',0]
            ]);
        }]);
        $order=['sort'=>'desc','update_time'=>'desc','goods_id'=>'desc'];
        $field="*";
        $append=['add_time_format'];
        $return = DataLogic::getDataList($goods_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        if($this->request->page==1){
            $category= GoodsCategoryModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_delete','=',0],
            ])->order(['sort'=>'desc','add_time'=>'desc'])
                ->field('category_id,name,pid')
                ->select()->toArray();
            $return['type_list']=$category;
        }
        return $this->success($return);
    }

    public function getChooseMemberList()
    {
        $keyword=$this->request->param('keyword');
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
        ];

        if(!empty($keyword)){
            if(false === strpos($keyword, '_')){
                $where[]=['mobile','=',$keyword];
            }else{
                $keyword_arr=explode("_", $keyword);
                $member_id=$keyword_arr[0];
                $where[]=['member_id','=',$member_id];
            }
        }


        $memberModel=new MemberModel();
        $memberModel=$memberModel->with(['level'=>function($query){
            $query->field('level_id,name,discount');
        }]);
        $order=['add_time'=>'desc'];
        $append=['add_time_format'];
        $field='member_id,nick_name,face,mobile,level_id,add_time';
        $data=DataLogic::getDataList($memberModel,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        //处理数据
        $lists=$data['list'];
        if(empty(!$lists)){
            $memberIds = $balances = $integrals = [];
            foreach($lists as $val){
                $memberIds[$val['member_id']] = $val['member_id'];
            }
            if(!empty($memberIds)){
                $MemberBalanceModel = new MemberBalanceModel();
                $balances =  $MemberBalanceModel->getBalanceSumByMemberId($memberIds);
                $MemberIntegralModel = new MemberIntegralModel();
                $integrals = $MemberIntegralModel->getIntegralSumByMemberId($memberIds);
            }
            foreach ($lists as $k => $list){
                $lists[$k]['balance']=$balances[$list['member_id']] ?? 0;
                $lists[$k]['integral']=$integrals[$list['member_id']] ?? 0;;
            }
        }
        $data['list']=$lists;
        return $this->success($data);
    }

    public function getMemberListName(){
        $keyword=$this->request->param('keyword');
        $member_id=$this->request->param('member_id/d');
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
        ];

        $where1 = '';
        if(!empty($keyword)){
            $where1 = 'nick_name like  "%'.$keyword.'%" or mobile like "%'.$keyword.'%"';
        }
        if(!empty($member_id)){
            $where[] = ['member_id','=',$member_id];
        }



        $memberModel=new MemberModel();
        $memberModel=$memberModel->with(['level'=>function($query){
            $query->field('level_id,name,discount');
        }])->where($where1);
        $order=['add_time'=>'desc'];
        $append=['add_time_format'];
        $field='member_id,nick_name,face,mobile,level_id,add_time';
        $data=DataLogic::getDataList($memberModel,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        //处理数据
        $lists=$data['list'];
        if(empty(!$lists)){
            $memberIds = $balances = $integrals = [];
            foreach($lists as $k=>$val){
                $memberIds[$val['member_id']] = $val['member_id'];
                $coupon_count = MemberCouponModel::where([
                        ['shop_id', '=', $this->request->shopId],
                        ['member_id', '=', $val['member_id']],
                        ['status', '=', 0],
                    ])->count() ?? 0;
                $lists[$k]['coupon_count']  = $coupon_count;

                $once_card_num = MemberOnceCardItemModel::where([
                        ['shop_id', '=', $this->request->shopId],
                        ['member_id', '=', $val['member_id']],
                        ['remain_num', '>', 0]
                    ])->count() ?? 0;
                $lists[$k]['once_card_num']  = $once_card_num;

                $snaup_up_num = MemberSnapUpModel::where([
                        ['shop_id', '=', $this->request->shopId],
                        ['member_id', '=', $val['member_id']]
                    ])->count() ?? 0 ;
                $lists[$k]['snaup_up_num']  = $snaup_up_num;


            }
            if(!empty($memberIds)){
                $MemberBalanceModel = new MemberBalanceModel();
                $balances =  $MemberBalanceModel->getBalanceSumByMemberId($memberIds);
                $MemberIntegralModel = new MemberIntegralModel();
                $integrals = $MemberIntegralModel->getIntegralSumByMemberId($memberIds);
            }
            foreach ($lists as $k => $list){
                $lists[$k]['balance']=$balances[$list['member_id']] ?? 0;
                $lists[$k]['integral']=$integrals[$list['member_id']] ?? 0;;
            }
        }
        $data['list']=$lists;
        return $this->success($data);
    }

    public function getChooseCouponList()
    {
        $member_id = $this->request->param('member_id/d',0);
        $total_price=$this->request->param('total_price/f',0);
        $today = date('Y-m-d', $this->request->time());
        $where = [
            ['shop_id','=',$this->request->shopId],
            ['member_id','=', $member_id],
            ['status','=',0],
            ['valid_end_time','>=',$today],
            ['money_limit','<=',$total_price]
        ];

        $member_coupon_model=new MemberCouponModel();
        $order=['member_coupon_id' => 'desc'];
        $append=['add_time_format'];
        $field="member_coupon_id,member_id,name,valid_bg_time,valid_end_time,coupon_money,money_limit,status,add_time";
        $return = DataLogic::getDataList($member_coupon_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }
}
